package com.atguigu.common.jwt;

import io.jsonwebtoken.*;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * ClassName: JwtHelper
 * Package: com.atguigu.common.jwt
 * Description:
 *
 * @Author 蹇泽阳
 * @Create 2023/11/16 10:05
 * @Version 1.0
 */
public class JwtHelper {
  
  private static long tokenExpiration = 365 * 24 * 60 * 60 * 1000; // 有效时间
  private static String tokenSignKey = "123456"; // 签名
  // 参数：用户Id，用户名字
  public static String createToken(Long userId, String username) {
    String token = Jwts.builder()
        // 分类
        .setSubject("AUTH-USER")
        // 设置token有效时间
        .setExpiration(new Date(System.currentTimeMillis() + tokenExpiration))
        // 设置主体部分
        .claim("userId", userId)
        .claim("username", username)
        
        .signWith(SignatureAlgorithm.HS512, tokenSignKey)
        .compressWith(CompressionCodecs.GZIP)
        .compact();
    return token;
  }
  // 从token获取用户id
  public static Long getUserId(String token) {
    try {
      if (StringUtils.isEmpty(token)) return null;
      
      Jws<Claims> claimsJws = Jwts.parser().setSigningKey(tokenSignKey).parseClaimsJws(token);
      Claims claims = claimsJws.getBody();
      Integer userId = (Integer) claims.get("userId");
      return userId.longValue();
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
  // 从token获取用户用户名称
  public static String getUsername(String token) {
    try {
      if (StringUtils.isEmpty(token)) return "";
      
      Jws<Claims> claimsJws = Jwts.parser().setSigningKey(tokenSignKey).parseClaimsJws(token);
      Claims claims = claimsJws.getBody();
      return (String) claims.get("username");
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
  
  public static void main(String[] args) {
    String token = JwtHelper.createToken(1L, "lisi");
    System.out.println(token);
    System.out.println(JwtHelper.getUserId(token));
    System.out.println(JwtHelper.getUsername(token));
  }
}